﻿@namespace MudBlazor
@implements IDisposable
@inherits MudComponentBase
@typeparam T

@*HEADER:*@
@if (HeaderTemplate != null && GroupDefinition != null)
{
<tr class="@HeaderClassname" @onclick="@OnRowClick" style="@HeaderStyle" @attributes="@UserAttributes">        
    @if ((GroupDefinition?.IsThisOrParentExpandable ?? false) || (Context?.Table.MultiSelection ?? false))
    {
        <MudTd>
            <div class="d-flex">
                @if (GroupDefinition.Expandable)
                {
                    <MudIconButton Class="mud-table-row-expander" Icon="@(IsExpanded ? ExpandIcon : CollapseIcon)" OnClick="@(() => IsExpanded = !IsExpanded)"/>
                }
                else
                {
                    if (GroupDefinition?.IsParentExpandable ?? false)
                    {
                        <div style="@ActionsStylename"></div>
                    }
                }
                @if (IsCheckable)
                {
                    <MudCheckBox T="bool?" @bind-Value="IsChecked" Class="mud-table-cell-checkbox"/>
                }
            </div>
        </MudTd>
    }
    @HeaderTemplate(new TableGroupData<object, T>(GroupDefinition.GroupName, Items.Key, Items.ToList()))
    @if ((Context?.Table.IsEditable ?? false))
    {
        <MudTd />
    }
</tr>
}
else
{
<MudText @attributes="@UserAttributes">@("There aren't any group definition to use with this component.")</MudText>
}

@if (IsExpanded)
{
    Context?.UpdateRowCheckBoxes();

    @*CHILDREN:*@
    @if (_innerGroupItems != null && GroupDefinition != null)
    {
        @foreach (var group in _innerGroupItems)
        {
            <MudTableGroupRow GroupDefinition="GroupDefinition.InnerGroup" Items="@group" IsCheckable="@IsCheckable"
                              HeaderClass="@HeaderClass" HeaderStyle="@HeaderStyle"
                              FooterClass="@FooterClass" FooterStyle="@FooterStyle"
                              HeaderTemplate="@HeaderTemplate" FooterTemplate="@FooterTemplate" T="T" @attributes="@UserAttributes" />
        }
    }
    else
    {
        @Table?.RenderRows(Items, (GroupDefinition?.Indentation ?? false) ? $"mud-table-row-group-indented-{GroupDefinition?.Level - 1}" : null, GroupDefinition?.IsThisOrParentExpandable ?? false)
    }

    @*FOOTER:*@
    @if (FooterTemplate != null && GroupDefinition != null)
    {
        <tr class="@FooterClassname" @onclick="@OnRowClick" style="@FooterStyle" @attributes="@UserAttributes">
            @if ((GroupDefinition?.IsThisOrParentExpandable ?? false) || (Context?.Table.MultiSelection ?? false))
            {
                <MudTd/>
            }
            @FooterTemplate(new TableGroupData<object, T>(GroupDefinition.GroupName, Items.Key, Items.ToList()))
            @if ((Context?.Table.IsEditable ?? false))
            {
                <MudTd />
            }

        </tr>
    }
}